<?php
/**
 * 随行付次日生效数据
 * User: 13651
 * Time: 2025/9/15 10:30
 */

namespace App\Console\Commands;


use App\Api\V2\Controller\User\StoreController;
use App\Repository\VbillRateSaveLogRepo;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Api\Controllers\Vbill\BaseController;
use Illuminate\Support\Facades\DB;

class VbillRateSaveLogCommand  extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'vbill-rate-save-log-command';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '随行付次日生效shell';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 执行脚本
     * @return void
     */
    public function handle()
    {
        $data = VbillRateSaveLogRepo::getInstance()->getQueryDateTimeList();

        if ($data->isEmpty())
            Log::info('随行付次日生效数据为空');

        $log = $data->toArray();

        /**
         * 开启事务
         */
        DB::beginTransaction();

        /**
         * 循环获取数据
         */
        foreach ($log as $k=>$v) {
            Log::info('随行付次日生效数据修改'.$v['id'].'开始执行');
            try {
                /**
                 * 更新费率
                 */
                $StoreObj = new StoreController();
                $StoreObj->send_ways_data(3,json_decode($v['save_param'],true));
                $status = 1;
            }catch (\Exception $e){
                Log::info('随行付次日生效数据修改,更新己方服务费率'.$v['id'].'失败');
                DB::rollBack();
                $status = 2;
            }

            /**
             * 修改成功后执行更改状态
             */
            $result = VbillRateSaveLogRepo::getInstance()->saveData(['status'=>$status],$v['id']);
            if (!$result)
            {
                Log::info('随行付次日生效数据修改记录ID:'.$v['id'].'失败');
                DB::rollBack();
                return;
            }
            DB::commit();
            Log::info('随行付次日生效数据修改状态为'.$status);
        }

    }
}